* Robustness check with balanced panel
* 4/13/2025

clear all
set more off

program main
	foreach design in sc state triple {
		balanced_panel, design(`design')
	}
	percent_effects
	make_table
	
	dynamic_regressions
end

program balanced_panel // balanced panel with years -5 through +5 around treatment
	syntax, design(str)
	
	local indiv_controls = "i.sex i.race i.hispan i.edcat age i.diffphys"
	local state_snap_controls = "i.has_bbce i.has_call i.has_faceini i.has_facerec i.has_oapp i.has_no_fp"
	
	use "$for_analysis/`design'_for_regressions_actual_ssi", clear
	keep if (startyear >= 2005 & startyear <= 2010) | mi(startyear)  // treated in 2005-2010 or control
	keep if inrange(relyr, -5, 5) | mi(relyr)
	save "$for_analysis/`design'_balanced_panel", replace
	
	if "`design'" == "sc" {
		ren inter inter_sc
		lab var inter_sc "Single x Post"
		qui su snap if relyr == -1 & treat == 1, d
		global base_sc = `r(mean)'
		reghdfe snap inter_sc [w = perwt], a(i.hhtype i.year) vce(cl statefip) // no controls
		est sto sc1
		qui estadd local cont " ", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE " ", replace
		qui estadd local yearFE "\checkmark", replace
		global beta_sc1 = _b[inter_sc] / $base_sc

		reghdfe snap inter_sc `indiv_controls' [w = perwt], ///
			a(i.hhtype i.year i.statefip) vce(cl statefip)	// controls
		est sto sc2
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace
		global beta_sc2 = _b[inter_sc] / $base_sc

		reghdfe snap inter_sc `indiv_controls' ///
			state_unemp perc_democrat_2004 hh_income_2003 `state_snap_controls' ///
			[w = perwt], a(i.hhtype i.year) vce(cl statefip) // state controls instead of state FE
		est sto sc3
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp "\checkmark", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE " ", replace
		qui estadd local yearFE "\checkmark", replace
		global beta_sc3 = _b[inter_sc] / $base_sc
	}
	else if "`design'" == "state" {
		lab var inter "Treat x Post"
		qui su snap if relyr == -1 & !mi(startyear), d
		global base_state = `r(mean)'
		reghdfe snap inter [w = perwt], a(i.statefip i.year) vce(cl statefip)	// no controls
		est sto state1
		qui estadd local cont " ", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype " ", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace
		global beta_state1 = _b[inter] / $base_state

		reghdfe snap inter `indiv_controls' [w = perwt], ///
			a(i.statefip i.year) vce(cl statefip)	// controls
		est sto state2
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype " ", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace
		global beta_state2 = _b[inter] / $base_state

		reghdfe snap inter `indiv_controls' ///
			state_unemp `state_snap_controls' ///
			[w = perwt], a(i.statefip i.year) vce(cl statefip) // add state controls
		est sto state3
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp "\checkmark", replace
		qui estadd local hhtype " ", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace
		global beta_state3 = _b[inter] / $base_state
	}
	else if "`design'" == "triple" {
		lab var triple "Single x Treated State x Post"
		qui su snap if relyr == -1 & treated == 1 & single == 1, d
		global base_triple = `r(mean)'
		
		reghdfe snap triple [w = perwt], ///
			a(i.hhtype##i.year i.year##i.statefip i.statefip##i.hhtype) vce(cl statefip)	// no controls, only FEs
		est sto triple1
		qui estadd local cont " ", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace
		global beta_triple1 = _b[triple] / $base_triple

		// note: state SNAP controls are collinear with i.year##i.statefip for triple diff, so need to omit
		reghdfe snap triple `indiv_controls' [w = perwt], ///
			a(i.hhtype##i.year i.year##i.statefip i.statefip##i.hhtype) vce(cl statefip)  // controls and FEs
		est sto triple2
		qui estadd local cont "\checkmark", replace
		qui estadd local unemp " ", replace
		qui estadd local hhtype "\checkmark", replace
		qui estadd local stateFE "\checkmark", replace
		qui estadd local yearFE "\checkmark", replace
		global beta_triple2 = _b[triple] / $base_triple
	}
end

program percent_effects
	di "baseline SNAP takeup rates:"
	di "sc: $base_sc"
	di "state: $base_state"
	di "triple: $base_triple"

	di "percent effects:"
	* twfe
	matrix percent_twfe = ${beta_state1}, ${beta_state2}, ${beta_state3}, ///
		${beta_sc1}, ${beta_sc2}, ${beta_sc3}, ${beta_triple1}, ${beta_triple2}
	mat li percent_twfe
end

program make_table
	esttab state1 state2 state3 sc1 sc2 sc3 triple1 triple2, ///
		keep(inter inter_sc triple) order(inter inter_sc triple) ///
		varlabels(inter "Treated x Post" inter_sc "Single x Post" triple "Single x Treated x Post") ///
		se(%5.3f) star(* 0.10 ** 0.05 *** 0.01) stats(yearFE stateFE hhtype cont unemp N, ///
		labels("Year FE" "State FE" "Household FE" "Individual Controls" "State Controls" "N" ) ///
		fmt("%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc")) scalars() ///
		nomtitles label nonote booktabs
		
	esttab state1 state2 state3 sc1 sc2 sc3 triple1 triple2 ///
		using "$output/balanced_twfe", replace ///
		keep(inter inter_sc triple) order(inter inter_sc triple) ///
		varlabels(inter "Treated x Post" inter_sc "Single x Post" triple "Single x Treated x Post") ///
		se(%5.3f) star(* 0.10 ** 0.05 *** 0.01) stats(yearFE stateFE hhtype cont unemp N, ///
		labels("Year FE" "State FE" "Household FE" "Individual Controls" "State Controls" "N" ) ///
		fmt("%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc")) scalars() ///
		nomtitles label nonote booktabs
end

program dynamic_regressions	
	local indiv_controls = "i.sex i.race i.hispan i.edcat age i.diffphys"
	local state_snap_controls = "i.has_bbce i.has_call i.has_faceini i.has_facerec i.has_oapp i.has_no_fp"
	local omit = 6
	
	use "$for_analysis/sc_balanced_panel", clear
	reghdfe snap ib`omit'.relyr_pos `indiv_controls' ///
		[w = perwt], a(i.hhtype i.year i.statefip) vce(cl statefip)
	est sto sc
	
	use "$for_analysis/state_balanced_panel", clear
	reghdfe snap ib`omit'.relyr_pos `indiv_controls' ///
		[w = perwt], a(i.statefip i.year) vce(cl statefip)
	est sto state
	
	coefplot (sc, offset(-0.06) keep(*.relyr_pos) ///
		ciopts(recast(rcap) lcolor(purple)) mcolor(purple) ///
		mfcolor(white) msymbol(T) msize(medsmall) label(Singles-Couples Design) baselevel) ///
		(state, offset(0.06) keep(*.relyr_pos) ///
		ciopts(recast(rcap) lcolor(midgreen)) label(State-level Design) ///
		mcolor(midgreen) mfcolor(white) msymbol(D) msize(medsmall)), ///
		xtitle("Years Since CAP Adopted") ytitle("Average Effect and 95% CI") vertical ///
		xscale(titlegap(2)) xline(5.5, lp(dash) lcolor(gs12)) yline(0, lwidth(thin) ///
		lp(dash) lcolor(black)) ylab(#15, angle(0)) graphregion(fcolor(white) lcolor(white) ///
		lwidth(vvvthin)) legend(region(lstyle(none))) ///
		coeflabel(Tm5 = "-5" Tm4 = "-4" Tm3 = "-3" Tm2 = "-2" Tm1 = "-1" Tp0 = "0" ///
		Tp1 = "1" Tp2 = "2" Tp3 = "3" Tp4 = "4" Tp5 = "5") omitted baselevel
	graph export "$output/balanced_panel.png", replace
end
	

*Execute
main
